package main

import (
	"fmt"
	"time"
)

func f1() {
	// 获取时间对象
	now := time.Now()

	// 获取格式化日期
	fmt.Printf("now: %v\n", now)
	fmt.Println("年", now.Year())
	fmt.Println("月", now.Month())
	fmt.Println("日", now.Day())
	fmt.Println("时", now.Hour())
	fmt.Println("分", now.Minute())
	fmt.Println("秒", now.Second())

	// 获取时间戳(19700101 8:00)
	timeStamp1 := now.Unix()
	timeStamp2 := now.UnixNano() //精确到纳秒
	fmt.Printf("timeStamp1: %v\n", timeStamp1)
	fmt.Printf("timeStamp2: %v\n", timeStamp2)

	// 时间戳解析
	timeStr := time.Unix(1660028148, 0)
	fmt.Printf("timeStr: %v\n", timeStr)

	// 时间间隔
	// 计算一个小时之后的时间
	fmt.Println(time.Second) // Golang Time包中定义的常量
	oneHourTime := now.Add(24 * time.Hour)
	fmt.Printf("oneHourTime: %v\n", oneHourTime)

	// 求两个时间的间隔
	d := now.Sub(oneHourTime)
	fmt.Printf("d: %v\n", d)

	// 定时器
	// timer := time.Tick(3 * time.Second)
	// for t := range timer {
	// 	fmt.Printf("t: %v\n", t)
	// }

	// 时间格式化
	//其它多数语言使用Y-m-d H:M:S的格式化模板，Go中使用Go诞生的时间：2006-1-2 15:04
	fmt.Println(now.Format("2006/01/02 15/04/05"))  // 24小时制
	fmt.Println(now.Format("2006-01-02 03:04:05 ")) // 12小时制
	fmt.Println(now.Format("2006-01-02 15:04:05.000"))

	// 按照对应格式解析字符串
	timeObj, err := time.Parse("2006-01-02", "2022-08-09")
	if err != nil {
		// 错误处理
		return
	}
	fmt.Printf("timeObj: %v\n", timeObj)

	// 系统睡眠
	time.Sleep(1000)
	time.Sleep(time.Second)
}
func f2() {
	// 获取当前时间
	nowTimeObj := time.Now()
	// 获取未来的时间
	// futerTime, err := time.Parse("2006-01-02 15:04", "2022-08-09 20:00")
	// if err != nil {
	// 	return
	// }
	// fmt.Printf("futerTime: %v\n", futerTime)
	// 指定时区
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		return
	}
	futerTime, err := time.ParseInLocation("2006-01-02 15:04", "2022-08-09 20:00", loc)
	fmt.Printf("timeObj: %v\n", futerTime)
	fmt.Printf("nowTimeObj: %v\n", nowTimeObj)
	// 时间对象相减
	td := nowTimeObj.Sub(futerTime)
	fmt.Printf("td: %v\n", td)

}
func main() {
	f2()
}
